package com.drug.mingkang.controller;
import com.drug.mingkang.model.HqImm;
import com.drug.mingkang.model.Inform;
import com.drug.mingkang.permission.model.PerUsers;
import com.drug.mingkang.service.HqImmService;
import com.drug.mingkang.service.InformService;
import com.drug.mingkang.utils.ResponseMessage;
import com.drug.mingkang.utils.Tools;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.google.common.math.BigIntegerMath;
import com.google.common.primitives.Longs;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.xml.soap.Text;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 通知公告(Inform)表控制层
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:14
 */
@RestController
@RequestMapping("inform")
public class InformController {
    /**
     * 服务对象
     */
    @Resource
    private InformService informService;

    ResponseMessage responseMessage = new ResponseMessage();

    @Resource
    private HqImmService hqImmService;

    /**
     * 通过主键查询单条数据
     *
     * @param informId 主键
     * @return 单条数据
     */
    @RequestMapping("selectOne")
    public Inform selectOne(String informId) {

        return this.informService.queryById(informId);

    }


    @RequestMapping("selectById")
    public Inform selectById(String informId) {
        return this.informService.selectById(informId);
    }

    /**
     * 显示列表
     *
     * @param inform
     * @return
     */
    @RequestMapping("selectAll")
    public Map<String, Object> selectAll(Inform inform, HttpSession session) {
        PerUsers oper = (PerUsers) session.getAttribute("perUsers");

        List<Inform> list = this.informService.queryAllByLimit(inform, oper.getCompanyId());

        for (int i = 0; i < list.size(); i++) {

            String immName = list.get(i).getImmName();

            String[] split = immName.split(",");

            String k = "";

            for (String li : split) {

                HqImm hqImm = hqImmService.queryById(li);

                k += hqImm.getImmName() + ",";
            }
            int a = k.lastIndexOf(",");

            String str = k.substring(0, a);

            list.get(i).setImmName(str);

        }

        int count = this.informService.selectCount(inform);
        Tools tools = new Tools();
        return tools.limit(list, count);
    }


    /**
     * 显示列表
     *
     * @param inform
     * @return
     */
    @RequestMapping("selectAllInform")
    public List<Inform> selectAllInform(Inform inform, HttpSession session) {

        PerUsers oper = (PerUsers) session.getAttribute("perUsers");

        List<Inform> list = this.informService.queryAllInform();

        if (oper.getCompanyId().equals("0")){
            return list;
        }

        List<Inform> listInform = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {

            String immName = list.get(i).getImmName();

            String[] split = immName.split(",");


            for (String li : split) {
                if (li.equals(oper.getCompanyId())) {
                    listInform.add(list.get(i));
                    break;
                }
            }
        }

        return listInform;
    }


    /**
     * 删除
     */
    @RequestMapping("delect")
    public ResponseMessage delete(String id) {
        int rows = informService.deleteById(id);
        if (rows > 0) {
            responseMessage.setStatus(1);
            responseMessage.setMessage("删除成功");
        } else {
            responseMessage.setStatus(0);
            responseMessage.setMessage("删除失败");
        }
        return responseMessage;
    }

    /**
     * 查询所有分公司
     */
    @RequestMapping("queryAll")
    public List<HqImm> queryAll(HqImm hqImm) {
        return hqImmService.queryAll(hqImm);
    }


    @RequestMapping("insertAor")
    public ResponseMessage insertAOr(Inform inform, HttpSession session) {

        int rows = 0;
        if (inform.getInformId() != null && !inform.getInformId().equals("")) {
            PerUsers oper = (PerUsers) session.getAttribute("perUsers");
            if (oper == null) {
                rows = 3;
            } else {
                rows = informService.update(inform);
            }
        } else {
            PerUsers oper = (PerUsers) session.getAttribute("perUsers");
            if (oper == null) {
                rows = 3;
            } else {
                inform.setOper(oper.getUserName());
                inform.setImmName(inform.getImmName());
                inform.setSort(1);
                if (!oper.getCompanyId().equals("0")) {
                    inform.setImmName(oper.getCompanyId());
                }
                rows = informService.insert(inform);
            }
        }
        if (rows == 3) {
            responseMessage.setStatus(2);
            responseMessage.setMessage("请先登陆");
        } else if (rows > 0) {
            responseMessage.setMessage("操作成功");
            responseMessage.setStatus(1);
        } else {
            responseMessage.setStatus(0);
            responseMessage.setMessage("操作失败");
        }
        return responseMessage;
    }

    @RequestMapping("selectIname")
    public Inform selectIname(HttpSession session) {

        PerUsers oper = (PerUsers) session.getAttribute("perUsers");

        List<Inform> list = informService.selectIname();   // 查完之后


        for (int i = 0; i < list.size(); i++) {
            String immName = list.get(i).getImmName();

            String[] split = immName.split(",");
            for (String li : split) {
                if (li.equals(oper.getCompanyId())) {
                    return list.get(i);
                }
            }
        }

        return null;
    }
}